/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import BE.FuncionalidadXPerfil;
import BE.Perfil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author CarLita
 */
public class PerfilDao {
    Session session;

    public int guardar(Perfil p) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(p);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
            session.close();
            return -1;
        } 
        
        String hql = "from Perfil perfil order by perfil.idPerfil desc";
        Query query = session.createQuery(hql);        
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return ((Perfil)resultList.get(0)).getIdPerfil();
        } catch (HibernateException he) {
            he.printStackTrace();
            session.close();
        } 
        return -1;
    }

    public Perfil buscarPorId(int idPerfil) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Perfil perfil where perfil.idPerfil = ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idPerfil);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Perfil)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        session.close();
        return null;
    }
    
    public List buscarFuncionalidadesPorIdPerfil(int idPerfil){
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from FuncionalidadXPerfil funcionalidad_x_perfil where funcionalidad_x_perfil.id.idPerfil= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idPerfil);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        session.close();
        return null;
    }

    public boolean guardarFuncionalidades(FuncionalidadXPerfil fxp) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(fxp);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
        } finally {
            session.close();
        }
        return ok;
    }

    public List<Perfil> buscarPerfiles() {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Perfil perfil";
        Query query = session.createQuery(hql);        
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        session.close();
        return null;
    }

    public boolean eliminar(int idPerfil) {
        boolean ok =true;
        Perfil perfil = buscarPorId(idPerfil);        
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(perfil);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;
    }
}
